namespace SubSonic
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Reflection;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CalendarControl : TextBox
    {
        private const string DEFAULT_FORMAT = "MM/dd/yyyy";
        private const string DEFAULT_INVALID_DATE = "Please enter a valid date.";
        private const string DEFAULT_JAVASCRIPT_FORMAT = "%m/%d/%Y %I:%M %p";
        private const string DEFAULT_LANGUAGE = "en";
        private string displayFormat;
        private Image imgCalendar;
        private string javaScriptFormat;
        private string language;
        private DateTime? selectedDate;
        private bool showTime = true;

        public CalendarControl()
        {
            this.DisplayFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
            if (!string.IsNullOrEmpty(this.DisplayFormat))
            {
                char ch = CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator[0];
                string[] strArray = this.DisplayFormat.Split(new char[] { ch });
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < strArray.Length; i++)
                {
                    builder.Append("%");
                    builder.Append(strArray[i][0]);
                    if ((i + 1) != strArray.Length)
                    {
                        builder.Append(ch);
                    }
                }
                this.JavaScriptFormat = builder.ToString().ToLower() + " %I:%M %p";
            }
            else
            {
                this.DisplayFormat = "MM/dd/yyyy";
                this.JavaScriptFormat = "%m/%d/%Y %I:%M %p";
            }
            this.Language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
        }

        protected override void OnPreRender(EventArgs e)
        {
            string text = "<link href='" + this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "SubSonic.Controls.Calendar.skin.theme.css") + "' rel='stylesheet' type='text/css' />";
            this.Page.Header.Controls.Add(new LiteralControl(text));
            this.Page.ClientScript.RegisterClientScriptInclude("CalendarMain", this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "SubSonic.Controls.Calendar.calendar.js"));
            this.Page.ClientScript.RegisterClientScriptInclude("CalendarSetup", this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "SubSonic.Controls.Calendar.calendar-setup.js"));
            if (string.IsNullOrEmpty(this.Language))
            {
                this.Language = "en";
            }
            if (Assembly.GetExecutingAssembly().GetManifestResourceStream("SubSonic.Controls.Calendar.lang.calendar-" + this.Language + ".js") == null)
            {
                this.Page.ClientScript.RegisterClientScriptInclude("CalendarLanguage", this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "SubSonic.Controls.Calendar.lang.calendar-en.js"));
            }
            else
            {
                this.Page.ClientScript.RegisterClientScriptInclude("CalendarLanguage", this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "SubSonic.Controls.Calendar.lang.calendar-" + this.Language + ".js"));
            }
            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.SelectedDate == DateTime.MinValue)
            {
                this.SelectedDate = new DateTime?(DateTime.Now);
            }
            writer.WriteLine("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
            writer.WriteLine("<tr>");
            writer.WriteLine("<td>");
            base.Render(writer);
            writer.WriteLine("</td><td>");
            this.CalendarImage.RenderControl(writer);
            writer.WriteLine("</td>");
            writer.WriteLine("</tr>");
            writer.WriteLine("</table>");
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "Calendar" + this.ClientID, string.Concat(new object[] { "<script type=\"text/javascript\">Calendar.setup( { inputField: \"", this.ClientID, "\", ifFormat: \"", this.JavaScriptFormat, "\", button: \"", this.CalendarImage.ClientID, "\", date: \"", this.SelectedDate, "\", showsTime: ", this.ShowTime ? "true" : "false", " } );</script>" }));
        }

        private Image CalendarImage
        {
            get
            {
                if (this.imgCalendar == null)
                {
                    this.imgCalendar = new Image();
                    this.imgCalendar.ID = "imgCalendar" + this.ClientID;
                    this.imgCalendar.ImageAlign = ImageAlign.AbsMiddle;
                    this.imgCalendar.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "SubSonic.Controls.Calendar.skin.calendar.gif");
                    this.imgCalendar.Style.Add("margin-left", "5px");
                }
                return this.imgCalendar;
            }
        }

        [DefaultValue("MM/dd/yyyy")]
        public string DisplayFormat
        {
            get
            {
                return this.displayFormat;
            }
            set
            {
                this.displayFormat = value;
            }
        }

        [DefaultValue("%m/%d/%Y %I:%M %p")]
        public string JavaScriptFormat
        {
            get
            {
                return this.javaScriptFormat;
            }
            set
            {
                this.javaScriptFormat = value;
            }
        }

        [DefaultValue("en")]
        public string Language
        {
            get
            {
                return this.language;
            }
            set
            {
                this.language = value;
            }
        }

        public DateTime? SelectedDate
        {
            get
            {
                string str = this.Text.Trim();
                if (!string.IsNullOrEmpty(str))
                {
                    this.selectedDate = new DateTime?(DateTime.Parse(str));
                }
                else
                {
                    this.selectedDate = null;
                }
                return this.selectedDate;
            }
            set
            {
                this.selectedDate = value;
                if (this.selectedDate.HasValue)
                {
                    this.Text = this.selectedDate.Value.ToString(this.DisplayFormat);
                }
                else
                {
                    this.Text = string.Empty;
                }
            }
        }

        public bool ShowTime
        {
            get
            {
                return this.showTime;
            }
            set
            {
                this.showTime = value;
            }
        }
    }
}

